
<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 基础表格
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column property="commodityName" label="商品名称" />
        <el-table-column property="commoditySalePrice" label="商品价格" />
        <el-table-column property="commodityDetails" label="商品描述" />
        <el-table-column property="commodityImg" label="商品图片" />
        <el-table-column property="commentCount" label="点赞评论数" />
        <el-table-column property="commoditySaleNum" label="商品销量" />
        <el-table-column property="commodityBorwseNum" label="商品游览数" />

        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
              >修改</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="Del(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

  
  </div>
</template>

<script>
import axios from "axios";
import { fetchData } from "../api/index";
export default {
  name: "basetable",
  data() {
    return {
      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 10,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      form: {
        commodityName: "",
        commoditySalePrice: "",
        commodityDetails: "",
        commodityImg: "",
        commentCount: "",
        commoditySaleNum: "",
        commodityBorwseNum: "",
      },
      idx: -1,
      id: -1,
    };
  },

  mounted() {
    this.Show();
  },
  methods: {
    Show() {
      axios({ url: "https://localhost:44310/api/Gshow" }).then((res) => {
        console.log(res);
        this.tableData = res.data;
      });
    },
    // 获取 easy-mock 的模拟数据
    getData() {
      fetchData(this.query).then((res) => {
        console.log(res);
        this.tableData = res.list;
        this.pageTotal = res.pageTotal || 50;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      console.log(index, row.id);
      sessionStorage.setItem("update", row.id);
      this.$router.push({ name: "upload", params: { id: row.id } }); //跳转到修改页面
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false;
      this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
     Del(index, row) {
console.log(index, row.id);
      if (confirm("确认删除吗")) {
        this.$axios({
          method: "DELETE",
          url: "https://localhost:44310/api/Gdel?gu=" + row.id,
        }).then((res) => {
          if (res.data.code == 204) {
            alert("删除成功");
            this.Show();
          } else {
            alert("删除成功");
            this.Show();
          }
        });
      }

    }
  },
};



</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>